package com.panfeng.xcloud.boss.provider.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.member.dao.mapper.UserInviterMapperExt;
import com.panfeng.xcloud.boss.provider.member.dto.UserInviterExtendItemDTO;
import com.panfeng.xcloud.boss.provider.member.dto.request.GetFamilyTreeInfoListPageReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.request.GetFamilyTreeInfoReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.request.QueryUserInviterPageReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.GetFamilyTreeInfoListRespDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.GetFamilyTreeInfoRespDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.QueryUserInviterPageRespDTO;
import com.panfeng.xcloud.boss.provider.member.service.IUserInviterService;
import com.panfeng.xcloud.common.core.page.PageResponse;
import com.panfeng.xcloud.common.core.utils.CollectionUtils;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.dao.base.MapperSupport;
import com.panfeng.xcloud.dao.member.entity.UserInviter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserInviterServiceImpl extends MapperSupport<Integer, UserInviter, UserInviterMapperExt> implements IUserInviterService {

    @Autowired
    private UserInviterMapperExt userInviterMapperExt;

    /**
     * 查询我的社区列表
     *
     * @param getFamilyTreeInfoListPageReqDTO
     * @return
     */
    @Override
    public List<GetFamilyTreeInfoListRespDTO> getFamilyTreeInfoList(GetFamilyTreeInfoListPageReqDTO getFamilyTreeInfoListPageReqDTO) {

        List<GetFamilyTreeInfoListRespDTO> familyTreeInfoList = userInviterMapperExt.getFamilyTreeInfoListPage(getFamilyTreeInfoListPageReqDTO);

        return familyTreeInfoList;
    }

    /**
     * 查询我的社区基本信息
     *
     * @param getFamilyTreeInfoReqDTO
     * @return
     */
    @Override
    public GetFamilyTreeInfoRespDTO getFamilyTreeInfo(GetFamilyTreeInfoReqDTO getFamilyTreeInfoReqDTO) {
        //  用户信息
        GetFamilyTreeInfoRespDTO familyTreeInfoVo = new GetFamilyTreeInfoRespDTO();
        //查询所有直推人的userId
        List<String> inviterUserIdList = userInviterMapperExt.listAllInviterUserIdByUserId(getFamilyTreeInfoReqDTO.getUserId());
        if (!CollectionUtils.isEmpty(inviterUserIdList)) {
            //直推人数
            familyTreeInfoVo.setInviterCount(inviterUserIdList.size());
            //查询已激活的直推人数
            familyTreeInfoVo.setInviterAliveCount(userInviterMapperExt.countAliveInviter(inviterUserIdList));
            //查询直推人今日盈利和总盈利
            Map<String, BigDecimal> map1 = userInviterMapperExt.sumTodayProfitAndTotalProfit(inviterUserIdList);
            if (null != map1) {
                familyTreeInfoVo.setInviterTodayProfit(map1.get("todayProfit"));
                familyTreeInfoVo.setInviterTotalProfit(map1.get("totalProfit"));
            }
        }
        //查询社区所有成员userId
        List<String> userIdList = userInviterMapperExt.listCommunityUserId(familyTreeInfoVo.getUserId());
        if (!CollectionUtils.isEmpty(userIdList)) {
            //社会人数
            familyTreeInfoVo.setCommunityCount(userIdList.size());
            //已激活社区人数
            familyTreeInfoVo.setCommunityAliveCount(userInviterMapperExt.countAliveInviter(userIdList));
            //今日总盈利和社区总盈利
            Map<String, BigDecimal> map2 = userInviterMapperExt.sumTodayProfitAndTotalProfit(userIdList);
            if (null != map2) {
                familyTreeInfoVo.setCommunityTodayProfit(map2.get("todayProfit"));
                familyTreeInfoVo.setCommunityTotalProfit(map2.get("totalProfit"));
            }
        }
        return familyTreeInfoVo;
    }

    /**
     * 查询直推人记录
     *
     * @param queryUserInviterPageReqDTO
     * @return
     */
    public PageResponse<QueryUserInviterPageRespDTO> queryUserInviter(QueryUserInviterPageReqDTO queryUserInviterPageReqDTO) {
        PageResponse<UserInviterExtendItemDTO> pageResponse = super.selectPage(queryUserInviterPageReqDTO, userInviterMapperExt::queryUserInviterExtPageCount, userInviterMapperExt::queryUserInviterExtPage);

        List<UserInviterExtendItemDTO> items = pageResponse.getItems();
        log.info(">>>> queryUserInviter-items.list：{} <<<", (null != items ? JSON.toJSONString(items) : ""));
        List<QueryUserInviterPageRespDTO> queryUserInviterRespDTOS = ConvertUtils.convert(items, QueryUserInviterPageRespDTO.class);

        PageResponse<QueryUserInviterPageRespDTO> pageFinalResponse = new PageResponse();
        pageFinalResponse.setTotalCount(pageResponse.getTotalCount());
        pageFinalResponse.measureTotalPage(pageResponse.getTotalCount(), pageResponse.getPageSize());
        pageFinalResponse.setItems(queryUserInviterRespDTOS);
        pageFinalResponse.setPageNumber(pageResponse.getPageNumber());
        pageFinalResponse.setPageSize(pageResponse.getPageSize());

        return pageFinalResponse;
    }

}
